package cn.hdu.liang.nio;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class FileChannelTest {
    public static void main(String[] args) {
        try(FileChannel inChannel = new RandomAccessFile("out/fromFile.txt", "rw").getChannel()) {

            ByteBuffer buffer = ByteBuffer.allocate(4);
            int bytesRead = inChannel.read(buffer);
            while(bytesRead != -1) {
                buffer.flip();
                while(buffer.hasRemaining()) {
                    System.out.println((char) buffer.get());
                }
                buffer.clear();
                bytesRead = inChannel.read(buffer);
            }

        } catch(IOException e) {
            e.printStackTrace();
        }

    }
}
